﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLite.Domain.Model;
using NLite.Data;

namespace NLite.Domain.Repository.Memory
{
    /// <summary>
    /// 内存仓储
    /// </summary>
    /// <typeparam name="TModel"></typeparam>
    [Component]
    public class Repository<TModel> : Repository<TModel,long>
        where TModel : class, IEntity<long>
    {

        protected static readonly List<TModel> Table;
        private static int counter;

        static Repository()
        {
            Table = new List<TModel>();
        }

        /// <summary>
        /// 更新或保存
        /// </summary>
        /// <param name="model"></param>
        protected static void InternalUpsert(TModel model)
        {
            lock (Table)
            {
                if (model.Id > 0)
                {
                    var target = Table.FirstOrDefault(p => p.Id == model.Id);
                    if (target == null)
                        throw new UpdateException("No record in db for id:" + model.Id);

                    Mapper.Map(model, ref target);
                }
                else
                {
                    counter++;
                    model.Id = counter;
                    Table.Add(model);
                }
            }
        }

        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="model"></param>
        public override void Add(TModel model)
        {
            InternalUpsert(model);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        public override void Delete(params long[] ids)
        {
            if (ids == null || ids.Length == 0)
                throw new ArgumentNullException("ids");
            if (Table.Count == 0)
                throw new DeleteException("No record in db.");

            lock (Table)
            {
                base.Delete(ids);
                //List<TModel> deletedModels = new List<TModel>(ids.Length);
                //foreach (var id in ids)
                //{
                //    var item = Table.FirstOrDefault(p => p.Id == id);
                //    if (item == null)
                //        throw new DeleteException("Record not exiests for id:" + id);
                //    deletedModels.Add(item);
                //}

                //foreach (var item in deletedModels)
                //    Table.Remove(item);
            }
        }

        public override void Delete(TModel model)
        {
            Table.Remove(model);
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        public override void Update(TModel model)
        {
            InternalUpsert(model);
        }


        ///// <summary>
        ///// 通过标志获取
        ///// </summary>
        ///// <param name="id"></param>
        ///// <returns></returns>
        //public virtual TModel Get(long id)
        //{
        //    return Table.FirstOrDefault(p => p.Id == id);
        //}

        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <returns></returns>
        public override IQueryable<TModel> Query()
        {
            return Table.AsQueryable<TModel>();
        }

        /// <summary>
        /// 分页、搜索、排序查询
        /// </summary>
        /// <param name="ctx"></param>
        /// <returns></returns>
        public override IPagination<TModel> Query(QueryContext ctx)
        {
            return Table.AsQueryable().ToPagination<TModel>(ctx);
        }
    }
    //[Component]
    //public class Repository<TModel> : IRepository<TModel>
    //    where TModel :class, IEntity<long>
    //{

    //    protected static readonly List<TModel> Table;
    //    private static int counter;

    //    static Repository()
    //    {
    //        Table = new List<TModel>();
    //    }

    //    /// <summary>
    //    /// 更新或保存
    //    /// </summary>
    //    /// <param name="model"></param>
    //    protected static void InternalUpsert(TModel model)
    //    {
    //        lock (Table)
    //        {
    //            if (model.Id > 0)
    //            {
    //                var target = Table.FirstOrDefault(p => p.Id == model.Id);
    //                if (target == null)
    //                    throw new UpdateException("No record in db for id:" + model.Id);

    //                Mapper.Map(model, ref target);
    //            }
    //            else
    //            {
    //                counter++;
    //                model.Id = counter;
    //                Table.Add(model);
    //            }
    //        }
    //    }

    //    /// <summary>
    //    /// 添加实体
    //    /// </summary>
    //    /// <param name="model"></param>
    //    public virtual void Add(TModel model)
    //    {
    //        InternalUpsert(model);
    //    }

    //    /// <summary>
    //    /// 批量删除
    //    /// </summary>
    //    /// <param name="ids"></param>
    //    public virtual void Delete(params long[] ids)
    //    {
    //        if (ids == null || ids.Length == 0)
    //            throw new ArgumentNullException("ids");
    //        if (Table.Count == 0)
    //            throw new DeleteException("No record in db.");

    //        lock (Table)
    //        {
    //            List<TModel> deletedModels = new List<TModel>(ids.Length);
    //            foreach (var id in ids)
    //            {
    //                var item = Table.FirstOrDefault(p => p.Id == id);
    //                if (item == null)
    //                    throw new DeleteException("Record not exiests for id:" + id);
    //                deletedModels.Add(item);
    //            }

    //            foreach (var item in deletedModels)
    //                Table.Remove(item);
    //        }
    //    }
    //    /// <summary>
    //    /// 更新
    //    /// </summary>
    //    /// <param name="model"></param>
    //    public virtual void Update(TModel model)
    //    {
    //        InternalUpsert(model);
    //    }


    //    /// <summary>
    //    /// 通过标志获取
    //    /// </summary>
    //    /// <param name="id"></param>
    //    /// <returns></returns>
    //    public virtual TModel Get(long id)
    //    {
    //        return Table.FirstOrDefault(p => p.Id == id);
    //    }

    //    /// <summary>
    //    /// 查询所有记录
    //    /// </summary>
    //    /// <returns></returns>
    //    public virtual IQueryable<TModel> Query()
    //    {
    //        return Table.AsQueryable<TModel>();
    //    }

    //    /// <summary>
    //    /// 分页、搜索、排序查询
    //    /// </summary>
    //    /// <param name="ctx"></param>
    //    /// <returns></returns>
    //    public virtual IPagination<TModel> Query(QueryContext ctx)
    //    {
    //        return Table.AsQueryable().ToPagination<TModel>(ctx);
    //    }
    //}
}
